#include<algorithm>
#include<cstdlib>
#include<fstream>
#include<iostream>
#include<iterator>
#include<locale>
#include<ostream>
#include<string>
#include<vector>


template<class Ch,class Tr,class OutIter>

void read(std::basic_istream<Ch,Tr>& in,OutIter iter)
{
    std::basic_string<Ch,Tr>line;
    while(std::getline(in,line))
    {
	*iter=line;
	++iter;
    }
}

template<typename Ch>
class sorter
{
    public:
	
	sorter(Ch const* locname):
	    loc_(std::locale(locname)),
	    collate_(std::use_facet<std::collate<Ch> >(loc_))
    {}
	sorter():
	    loc_(std::locale()),
	    collate_(std::use_facet<std::collate<Ch> >(loc_))
    {}
	template<typename Tr>
	    bool operator()(const std::basic_string<Ch,Tr>&lhs,
			    const std::basic_string<Ch,Tr>&rhs)
	    {
		return collate_.compare(lhs.data(),lhs.data()+lhs.size(),
					rhs.data(),rhs.data()+rhs.size())<0;
	    }
    private:
	std::locale loc_;
	const std::collate<Ch>&collate_;
};


template<typename Ch>
sorter<Ch> make_sorter(Ch const * name)
{
    return sorter<Ch>(name);
}



int main(int argc,char* argv[])
	try
{
    std::cin.exceptions(std::ios_base::badbit);

    std::vector<std::string> text;
    if(argc<2)
      read(std::cin,std::back_inserter(text));
    else
    {
	std::ifstream in(argv[1]);
	if(not in)
	{
	    std::perror(argv[1]);
	    return EXIT_FAILURE;
	}
	read(in,std::back_inserter(text));
    }


    std::sort(text.begin(),text.end(),make_sorter(argc>=3?argv[2]:""));

    std::copy(text.begin(),text.end(),
		std::ostream_iterator<std::string>(std::cout,"\n"));
}

catch (std::exception& ex)
{
    std::cerr<<"Caught exceptio:"<<ex.what()<<'\n';
    std::cerr<<"Terminating program.\n";
    std::exit(EXIT_FAILURE);
}

catch (...)
{
    std::cerr<<"Caught unknown excpetion type.\nTerminating program.\n";
    std::exit(EXIT_FAILURE);
}



	
